%% Set up the Import Options and import the data
opts = spreadsheetImportOptions("NumVariables", 13);

% Specify sheet and range
opts.Sheet = "Sheet1";
opts.DataRange = "A2:M65";

% Specify column names and types
opts.VariableNames = ["Source", "Sink", "words", "nonwords", "Difference", "psame", "Sig", "VarName8", "k", "kalpham", "FDRSig", "pmk", "FDRadj"];
opts.VariableTypes = ["string", "string", "double", "double", "double", "double", "string", "string", "double", "double", "string", "double", "double"];

% Specify variable properties
opts = setvaropts(opts, "VarName8", "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["Source", "Sink", "Sig", "VarName8", "FDRSig"], "EmptyFieldRule", "auto");

% Import the data
LexicalityDecodingROIs400500mswordsnonwordsFDRcorrected1 = readtable("/autofs/space/clive_001/users/olivia/GE_new/Granger/results/Lexicality_Decoding_ROIs_400-500ms_words-nonwords_FDRcorrected.xlsx", opts, "UseExcel", false);


%% Clear temporary variables
clear opts

% Convert Table to Struct
pvalstruct = table2struct(LexicalityDecodingROIs400500mswordsnonwordsFDRcorrected1);

%Ordered ROI list for arranging the matrix
ROIs = {'L-STG_1', 'L-MTG_2', 'R-STG_1', 'R-STG_3', 'R-STG_2', 'R-MTG_1', 'R-MTG_2', 'R-ITG_1', 'R-ITG_2', 'R-postCG_2'};
pubROIs = {'L-STG_1', 'L-MTG_1', 'R-STG_1', 'R-STG_2', 'R-STG_3', 'R-MTG_2', 'R-MTG_3', 'R-ITG_2', 'R-ITG_3', 'R-postCG_5'};
%make the colormap
riter = (1-(216/255))/15;
giter = (1-(27/255))/15;
biter = (1-(96/255))/15;
pinkwhite = [216/255, 27/255, 96/255];
for k = 1:15
pinkwhite(k+1, :) = pinkwhite(k, :) + [riter, giter, biter];
end
riter_blue = (1-(30/255))/15;
giter_blue = (1-(136/255))/15;
biter_blue = (1-(229/255))/15;
bluewhite = [1,1,1];
for k = 1:15
bluewhite(k+1, :) = bluewhite(k, :) - [riter_blue, giter_blue, biter_blue];
end
redblue = [pinkwhite; bluewhite];

% Build the matrix form of it
arrayform = [];
x = [];
y = [];
c = [];
for kcomp = 1:length(pvalstruct)
if matches(pvalstruct(kcomp).FDRSig, '*')
srcROIidx = find(matches(ROIs, pvalstruct(kcomp).Source));
sinkROIidx = find(matches(ROIs, pvalstruct(kcomp).Sink));
arrayform(srcROIidx, sinkROIidx) = pvalstruct(kcomp).Difference;
valence = sign(pvalstruct(kcomp).Difference);
x = horzcat(x, [sinkROIidx-0.5; sinkROIidx; sinkROIidx+0.5]);
y = horzcat(y, [srcROIidx+(0.5*valence); srcROIidx-(0.5*valence); srcROIidx+(0.5*valence)]);
c = [c; pvalstruct(kcomp).Difference];
end
end
figure; image(arrayform, 'CDataMapping', 'scaled');
colormap(single(redblue));
set(gca, 'CLim', [-max(abs(arrayform(:))), max(abs(arrayform(:)))]);
set(gca, 'xaxislocation', 'top');
set(gca, 'XTick', [1:10], 'XTickLabels', pubROIs, 'YTickLabels', pubROIs);
xlabel('Sink'); ylabel('Source'); colorbar;

figure; patch(x, y, c, 'CDataMapping', 'scaled', 'EdgeColor', 'none'); colormap(single(redblue));
set(gca, 'YDir', 'reverse');
set(gca, 'xaxislocation', 'top');
set(gca, 'XTick', [1:10], 'XTickLabels', pubROIs, 'YTickLabels', pubROIs);
xlabel('Sink'); ylabel('Source');
cb = colorbar('eastoutside'); cb.Label.String = {'$$ \textsf{Dif{}ference in Suprathreshold GCi Count} $$', '$$\ \textsf{(words} \bigtriangleup \textsf{- nonwords} \bigtriangledown \textsf{)}$$'}; cb.Label.Interpreter = 'latex'; cb.Label.Rotation = 270; cb.Label.VerticalAlignment='bottom';
set(gca, 'XTick', [1:10], 'XTickLabels', pubROIs, 'YTick', [1:10], 'YTickLabels', pubROIs);
set(gca, 'XLim', [0.5, 10.5], 'YLim', [0.5, 10.5]);
set(gca, 'CLim', [-max(abs(arrayform(:))), max(abs(arrayform(:)))]);
box on;

%% Words only heatmap
wordsonlyarray = [];
for kcomp = 2:length(pvalstruct)
srcROIidx = find(matches(ROIs, cellstr(pvalstruct(kcomp).Source)));
sinkROIidx = find(matches(ROIs, cellstr(pvalstruct(kcomp).Sink)));
wordsonlyarray(srcROIidx, sinkROIidx) = pvalstruct(kcomp).nonwords;
end
figure; imagesc(wordsonlyarray);
colormap('hot'); %uses matlab preset colormap
set(gca, 'xaxislocation', 'top');
set(gca, 'XTick', [1:10], 'XTickLabels', pubROIs, 'YTickLabels', pubROIs);
xlabel('Sink'); ylabel('Source'); colorbar;